//
// Created by 33145 on 2023/6/8.
//

#ifndef ROCKET_RPC_CONTROLLER_H
#define ROCKET_RPC_CONTROLLER_H

#include <google/protobuf/service.h>
#include <google/protobuf/stubs/callback.h>
#include <string>

#include "../tcp/net_addr.h"

namespace rocket {
  // 获取连接地址，控制超时等
  class RPCController : public google::protobuf::RpcController {

  public:
    typedef std::shared_ptr<RPCController> ptr;
    RPCController() = default;
    ~RPCController() = default;

    // 重载函数
    void Reset() override;    // 用于客户端调用 清除RPC状态
    bool Failed() const override;   // 是否成功
    std::string ErrorText() const override;
    void StartCancel() override;
    void SetFailed(const std::string& reason) override;
    bool IsCanceled() const override;
    void NotifyOnCancel(google::protobuf::Closure* callback) override;


    void SetError(int32_t error_code, const std::string& error_info);
    int32_t GetErrorCode() const;
    std::string GetErrorInfo();
    void SetMsgId(const std::string& msg_id);
    std::string GetMsgId();
    void SetLocalAddr(NetAddr::ptr addr);
    void SetPeerAddr(NetAddr::ptr addr);
    NetAddr::ptr GetLocalAddr();
    NetAddr::ptr GetPeerAddr();
    void SetTimeout(int timeout);
    int GetTimeout();
    bool Finished();
    void SetFinished(bool value);

  private:
    int32_t _error_code {0};
    std::string _error_info;
    std::string _msg_id;

    bool _is_failed {false};      // RPC调用结果
    bool _is_cancel {false};      // 是否取消RPC调用
    bool _is_finished {false};

    NetAddr::ptr _local_addr;
    NetAddr::ptr _peer_addr;

    int _timeout {1000};   // ms
  };
}

#endif //ROCKET_RPC_CONTROLLER_H
